{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['NDB_No', 'Shrt_Desc', 'Water_(g)', 'Energ_Kcal', 'Protein_(g)', 'Lipid_Tot_(g)', 'Ash_(g)', 'Carbohydrt_(g)', 'Fiber_TD_(g)', 'Sugar_Tot_(g)', 'Calcium_(mg)', 'Iron_(mg)', 'Magnesium_(mg)', 'Phosphorus_(mg)', 'Potassium_(mg)', 'Sodium_(mg)', 'Zinc_(mg)', 'Copper_(mg)', 'Manganese_(mg)', 'Selenium_(mcg)', 'Vit_C_(mg)', 'Thiamin_(mg)', 'Riboflavin_(mg)', 'Niacin_(mg)', 'Vit_B6_(mg)', 'Vit_B12_(mcg)', 'Vit_A_IU', 'Vit_A_RAE', 'Vit_E_(mg)', 'Vit_D_mcg', 'Vit_D_IU', 'Vit_K_(mcg)', 'FA_Sat_(g)', 'FA_Mono_(g)', 'FA_Poly_(g)', 'Cholestrl_(mg)']\n",
      "   NDB_No                 Shrt_Desc  Water_(g)  Energ_Kcal  Protein_(g)  \\\n",
      "0    1001          BUTTER WITH SALT      15.87         717         0.85   \n",
      "1    1002  BUTTER WHIPPED WITH SALT      15.87         717         0.85   \n",
      "2    1003      BUTTER OIL ANHYDROUS       0.24         876         0.28   \n",
      "\n",
      "   Lipid_Tot_(g)  Ash_(g)  Carbohydrt_(g)  Fiber_TD_(g)  Sugar_Tot_(g)  \\\n",
      "0          81.11     2.11            0.06           0.0           0.06   \n",
      "1          81.11     2.11            0.06           0.0           0.06   \n",
      "2          99.48     0.00            0.00           0.0           0.00   \n",
      "\n",
      "        ...        Vit_A_IU  Vit_A_RAE  Vit_E_(mg)  Vit_D_mcg  Vit_D_IU  \\\n",
      "0       ...          2499.0      684.0        2.32        1.5      60.0   \n",
      "1       ...          2499.0      684.0        2.32        1.5      60.0   \n",
      "2       ...          3069.0      840.0        2.80        1.8      73.0   \n",
      "\n",
      "   Vit_K_(mcg)  FA_Sat_(g)  FA_Mono_(g)  FA_Poly_(g)  Cholestrl_(mg)  \n",
      "0          7.0      51.368       21.021        3.043           215.0  \n",
      "1          7.0      50.489       23.426        3.012           219.0  \n",
      "2          8.6      61.924       28.732        3.694           256.0  \n",
      "\n",
      "[3 rows x 36 columns]\n"
     ]
    }
   ],
   "source": [
    "import pandas\n",
    "food_info = pandas.read_csv(\"food_info.csv\")\n",
    "col_names = food_info.columns.tolist()\n",
    "print(col_names)\n",
    "print(food_info.head(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#print food_info[\"Iron_(mg)\"]\n",
    "#div_1000 = food_info[\"Iron_(mg)\"] / 1000\n",
    "#print div_1000\n",
    "# Adds 100 to each value in the column and returns a Series object.\n",
    "#add_100 = food_info[\"Iron_(mg)\"] + 100\n",
    "\n",
    "# Subtracts 100 from each value in the column and returns a Series object.\n",
    "#sub_100 = food_info[\"Iron_(mg)\"] - 100\n",
    "\n",
    "# Multiplies each value in the column by 2 and returns a Series object.\n",
    "#mult_2 = food_info[\"Iron_(mg)\"]*2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#It applies the arithmetic operator to the first value in both columns, the second value in both columns, and so on\n",
    "water_energy = food_info[\"Water_(g)\"] * food_info[\"Energ_Kcal\"]\n",
    "water_energy = food_info[\"Water_(g)\"] * food_info[\"Energ_Kcal\"]\n",
    "iron_grams = food_info[\"Iron_(mg)\"] / 1000  \n",
    "food_info[\"Iron_(g)\"] = iron_grams"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#Score=2×(Protein_(g))−0.75×(Lipid_Tot_(g))\n",
    "weighted_protein = food_info[\"Protein_(g)\"] * 2\n",
    "weighted_fat = -0.75 * food_info[\"Lipid_Tot_(g)\"]\n",
    "initial_rating = weighted_protein + weighted_fat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# the \"Vit_A_IU\" column ranges from 0 to 100000, while the \"Fiber_TD_(g)\" column ranges from 0 to 79\n",
    "#For certain calculations, columns like \"Vit_A_IU\" can have a greater effect on the result, \n",
    "#due to the scale of the values\n",
    "# The largest value in the \"Energ_Kcal\" column.\n",
    "max_calories = food_info[\"Energ_Kcal\"].max()\n",
    "# Divide the values in \"Energ_Kcal\" by the largest value.\n",
    "normalized_calories = food_info[\"Energ_Kcal\"] / max_calories\n",
    "normalized_protein = food_info[\"Protein_(g)\"] / food_info[\"Protein_(g)\"].max()\n",
    "normalized_fat = food_info[\"Lipid_Tot_(g)\"] / food_info[\"Lipid_Tot_(g)\"].max()\n",
    "food_info[\"Normalized_Protein\"] = normalized_protein\n",
    "food_info[\"Normalized_Fat\"] = normalized_fat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "760     0.0\n",
      "610     0.0\n",
      "611     0.0\n",
      "8387    0.0\n",
      "8607    0.0\n",
      "629     0.0\n",
      "630     0.0\n",
      "631     0.0\n",
      "6470    0.0\n",
      "654     0.0\n",
      "8599    0.0\n",
      "633     0.0\n",
      "634     0.0\n",
      "635     0.0\n",
      "637     0.0\n",
      "638     0.0\n",
      "639     0.0\n",
      "646     0.0\n",
      "653     0.0\n",
      "632     0.0\n",
      "606     0.0\n",
      "6463    0.0\n",
      "655     0.0\n",
      "673     0.0\n",
      "658     0.0\n",
      "3636    0.0\n",
      "659     0.0\n",
      "660     0.0\n",
      "661     0.0\n",
      "3663    0.0\n",
      "       ... \n",
      "8153    NaN\n",
      "8155    NaN\n",
      "8156    NaN\n",
      "8157    NaN\n",
      "8158    NaN\n",
      "8159    NaN\n",
      "8160    NaN\n",
      "8161    NaN\n",
      "8163    NaN\n",
      "8164    NaN\n",
      "8165    NaN\n",
      "8167    NaN\n",
      "8169    NaN\n",
      "8170    NaN\n",
      "8172    NaN\n",
      "8173    NaN\n",
      "8174    NaN\n",
      "8175    NaN\n",
      "8176    NaN\n",
      "8177    NaN\n",
      "8178    NaN\n",
      "8179    NaN\n",
      "8180    NaN\n",
      "8181    NaN\n",
      "8183    NaN\n",
      "8184    NaN\n",
      "8185    NaN\n",
      "8195    NaN\n",
      "8251    NaN\n",
      "8267    NaN\n",
      "Name: Sodium_(mg), dtype: float64\n",
      "276     38758.0\n",
      "5814    27360.0\n",
      "6192    26050.0\n",
      "1242    26000.0\n",
      "1245    24000.0\n",
      "1243    24000.0\n",
      "1244    23875.0\n",
      "292     17000.0\n",
      "1254    11588.0\n",
      "5811    10600.0\n",
      "8575     9690.0\n",
      "291      8068.0\n",
      "1249     8031.0\n",
      "5812     7893.0\n",
      "1292     7851.0\n",
      "293      7203.0\n",
      "4472     7027.0\n",
      "4836     6820.0\n",
      "1261     6580.0\n",
      "3747     6008.0\n",
      "1266     5730.0\n",
      "4835     5586.0\n",
      "4834     5493.0\n",
      "1263     5356.0\n",
      "1553     5203.0\n",
      "1552     5053.0\n",
      "1251     4957.0\n",
      "1257     4843.0\n",
      "294      4616.0\n",
      "8613     4450.0\n",
      "         ...   \n",
      "8153        NaN\n",
      "8155        NaN\n",
      "8156        NaN\n",
      "8157        NaN\n",
      "8158        NaN\n",
      "8159        NaN\n",
      "8160        NaN\n",
      "8161        NaN\n",
      "8163        NaN\n",
      "8164        NaN\n",
      "8165        NaN\n",
      "8167        NaN\n",
      "8169        NaN\n",
      "8170        NaN\n",
      "8172        NaN\n",
      "8173        NaN\n",
      "8174        NaN\n",
      "8175        NaN\n",
      "8176        NaN\n",
      "8177        NaN\n",
      "8178        NaN\n",
      "8179        NaN\n",
      "8180        NaN\n",
      "8181        NaN\n",
      "8183        NaN\n",
      "8184        NaN\n",
      "8185        NaN\n",
      "8195        NaN\n",
      "8251        NaN\n",
      "8267        NaN\n",
      "Name: Sodium_(mg), dtype: float64\n"
     ]
    }
   ],
   "source": [
    "#By default, pandas will sort the data by the column we specify in ascending order and return a new DataFrame\n",
    "# Sorts the DataFrame in-place, rather than returning a new DataFrame.\n",
    "#print food_info[\"Sodium_(mg)\"]\n",
    "food_info.sort_values(\"Sodium_(mg)\", inplace=True)\n",
    "print food_info[\"Sodium_(mg)\"]\n",
    "#Sorts by descending order, rather than ascending.\n",
    "food_info.sort_values(\"Sodium_(mg)\", inplace=True, ascending=False)\n",
    "print food_info[\"Sodium_(mg)\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
